  <template>
    <div class="login-container">
      <el-card class="login-card">
        <div class="title">智能小车管理系统</div>
        <el-tabs v-model="activeTab" @tab-click="handleClick">
          <el-tab-pane label="登录" name="login">
            <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-form">
              <el-form-item prop="username">
                <el-input v-model="loginForm.account" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password" placeholder="请输入密码"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleLogin" class="login-button" :loading="loading">登录</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="注册" name="register">
            <el-form :model="registerForm" :rules="rules" ref="registerForm" class="login-form">
              <el-form-item prop="username">
                <el-input v-model="registerForm.account" prefix-icon="el-icon-user" placeholder="请输入账号"></el-input>
              </el-form-item>
              <el-form-item prop="name">
                <el-input v-model="registerForm.name" prefix-icon="el-icon-user" placeholder="请输入姓名"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input v-model="registerForm.password" prefix-icon="el-icon-lock" type="password" placeholder="请输入密码"></el-input>
              </el-form-item>
              <el-form-item prop="confirmPassword">
                <el-input v-model="registerForm.confirmPassword" prefix-icon="el-icon-lock" type="password" placeholder="请确认密码"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleRegister" class="login-button" :loading="loading">注册</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </template>

  <script>
  import { user, register } from '@/api/system/user'

  export default {
    name: 'LoginPage',
    data() {
      const validateConfirmPassword = (rule, value, callback) => {
        if (value !== this.registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      }
      return {
        activeTab: 'login',
        loading: false,
        loginForm: {
          account: '',
          password: ''
        },
        registerForm: {
          account: '',
          name: '',
          password: '',
          confirmPassword: ''
        },
        rules: {
          account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
          name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
          password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
          confirmPassword: [
            { required: true, message: '请确认密码', trigger: 'blur' },
            { validator: validateConfirmPassword, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      handleClick(tab) {
        this.activeTab = tab.name
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            user(this.loginForm).then((res) => {
              if (res.code === 200) {
                this.$message.success('登录成功')
                this.$router.push('/dashboard')
              } else {
                this.$message.error(res.msg || '登录失败，请检查用户名和密码')
              }
            }).catch((error) => {
              this.$message.error(error.response?.data?.msg || '登录失败，请检查网络连接')
            }).finally(() => {
              this.loading = false
            })
          }
        })
      },
      handleRegister() {
        this.$refs.registerForm.validate(valid => {
          if (valid) {
            this.loading = true
            const registerData = {
              account: this.registerForm.account,
              name: this.registerForm.name,
              password: this.registerForm.password
            }
            register(registerData).then((res) => {
              if (res.code === 200) {
                this.$message.success('注册成功，请登录')
                this.activeTab = 'login'
                this.registerForm.account = ''
                this.registerForm.name = ''
                this.registerForm.password = ''
                this.registerForm.confirmPassword = ''
              } else {
                this.$message.error(res.msg || '注册失败，请稍后重试')
              }
            }).catch((error) => {
              this.$message.error(error.response?.msg || error.msg || '注册失败，请检查网络连接')
            }).finally(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
  </script>

  <style scoped>
  .login-container {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f3f3f3;
  }

  .login-card {
    width: 400px;
  }

  .title {
    text-align: center;
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 30px;
    color: #409EFF;
  }

  .login-form {
    padding: 20px;
  }

  .login-button {
    width: 100%;
  }
  </style>